package com.lzb.yabbs;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by zaibinng on 2015/9/13.
 */
public class SwipeLoadMoreLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener{

    private int mTouchSlop;     //滑动到最上面的上啦操作
    private ListView mListView;
    private OnLoadListener mOnLoadListener; //上啦监听器，到了最底部的上啦操作
    private View mListViewFooter;

    private int mYDown; //按下的Y坐标
    private int mLastY; //抬起时的Y坐标，与mYDown一起用于滑动到底部时判断是上啦还是下拉
    private boolean isLoading =false;



    public SwipeLoadMoreLayout(Context context) {
        super(context);
    }

    public SwipeLoadMoreLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop();
        mListViewFooter= LayoutInflater.from(context).inflate(R.layout.listview_footer,null,false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if(mListView==null){
            getListView();
        }
    }

    private void getListView() {
        int childs=getChildCount();
        if(childs>0){
            View childView=getChildAt(0);
            if(childView instanceof ListView){
                mListView= (ListView) childView;
                mListView.setOnScrollListener(this);
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        final int action=ev.getAction();

        switch (action){
            case MotionEvent.ACTION_DOWN:
                mYDown= (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                mLastY= (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                if(canLoad()){
                    loadData();
                }
                break;
            default:
                break;
        }

        return super.dispatchTouchEvent(ev);
    }

    private void loadData() {
        if(mOnLoadListener!=null){
            setLoading(true);
            mOnLoadListener.onLoad();
        }
    }

    public void setLoading(boolean isLoading) {
        this.isLoading=isLoading;

        if(isLoading){
            mListView.addFooterView(mListViewFooter);
        }else {
            mListView.removeFooterView(mListViewFooter);
            mYDown=0;
            mLastY=0;
        }
    }

    public void setmOnLoadListener(OnLoadListener onLoadListener){
        this.mOnLoadListener=onLoadListener;
    }

    /**
     * 判断是否可以加载更多
     * @return
     */
    private boolean canLoad() {

        return isBottom()&&!isLoading&&isPullUp();
    }

    private boolean isPullUp() {

        return (mYDown-mLastY)>=mTouchSlop;
    }

    private boolean isBottom() {
        if(mListView!=null&&mListView.getAdapter()!=null){
            return mListView.getLastVisiblePosition()==(mListView.getAdapter().getCount()-1);
        }
        return false;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if(canLoad()){
            loadData();
        }
    }

}
